﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class UFXCompletingCheckDAL
    {
        public static bool Add(UFXCompletingCheckInfo obj, List<UFXCompletingCheckNGInfo> NGItems)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UFXCompletingCheck_Add", obj.SerialNumber,obj.MachineType,obj.CheckDate,obj.CreateDate,obj.EngineNumber,obj.OperationMode, obj.Cab,obj.Track,obj.Bucket,obj.Platform,obj.FrameUnder,obj.Forearm,obj.TeShuPeiGuan,obj.GPSNumber,obj.TuiTuBan,obj.HeGe,obj.BengNum,obj.DuoLuFaNum,obj.HuiZhuanMaDaNum,obj.GunPanNum,obj.ZYHZJTNum,obj.YYYXBHNum,obj.RYXBHNum,obj.XZMDZNum,obj.XZMDYNum,obj.TTBCYLZNum,obj.TTBCYLYNum,obj.DBCYLNum,obj.XBCYLNum,obj.CDCYLNum,obj.DBBHNum,obj.XBBHNum,obj.YouWen,obj.ShuiWen,obj.YYYQJD,obj.ZDZS,obj.QDQDJItem,obj.XNJCItem,obj.GZQRItem,obj.CheckMan,obj.KTBHNum,obj.DBCYLNumRight,obj.DQWD, obj.YMFZZS, obj.QSSW, obj.QSYYYW, obj.GZYXSHYWSW);
            try
            {

                int fromid = Convert.ToInt32(DB.ExecuteScalar(dbCommand));
                //机号已存在
                if (fromid <= 0)
                {
                    return false;
                }
                foreach (UFXCompletingCheckNGInfo item in NGItems)
                {
                    item.CCID = fromid;

                    DbCommand dbCommandNG = DB.GetStoredProcCommand("UFXCompletingCheckNG_Add", item.CCID, item.WT, item.YYFX, item.JG, item.OK, item.QR, item.Type);
                    DB.ExecuteNonQuery(dbCommandNG);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Delete(int CCID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UFXCompletingCheck_Delete", CCID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
     //   public static IDataReader GetBySerialNumber(string SerialNumber)
	    //{
		   // Database DB = DatabaseFactory.CreateDatabase();		
		
     //       DbCommand dbCommand = DB.GetStoredProcCommand("UFXCompletingCheck_GetBySerialNumber", SerialNumber);
     //       return DB.ExecuteReader(dbCommand);
	    //}

        public static IDataReader GetById(int id)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UFXCompletingCheck_GetById", id);
            return DB.ExecuteReader(dbCommand);
        }
        public static IDataReader UFXCompletingCheckNG_GetById(int id)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UFXCompletingCheckNG_GetById", id);
            return DB.ExecuteReader(dbCommand);
        }

        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UFXCompletingCheck_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
				            DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt16( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }


        //public static DataTable OCompletingCheck_ExportNGListByDate(string FormDate, string ToDate)
        //{
        //    Database DB = DatabaseFactory.CreateDatabase();

        //    DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_ExportNGListByDate", FormDate, ToDate);
        //    return DB.ExecuteDataSet(dbCommand).Tables[0];
        //}
        ///// <summary>
        ///// 判断完成检查书中，相关号码是否已存在
        ///// </summary>
        ///// <returns></returns>
        //public static int OCompletingCheck_GetCheckUniqueNum(string ColumnName, string ColumnValue)
        //{
        //    Database DB = DatabaseFactory.CreateDatabase();

        //    DbCommand dbCommand = DB.GetStoredProcCommand("OCompletingCheck_GetCheckUniqueNum", ColumnName, ColumnValue);
        //    return Convert.ToInt32( DB.ExecuteScalar(dbCommand));
        //}
    }
}
